package zwylinkedlist;

import java.util.LinkedList;

import org.junit.Test;

public class LinkedList03Test {

	@Test
	public void testAddFirstLast() {
		LinkedList<String> dataList = new LinkedList<>();
		dataList.addFirst("1");
		
		dataList.addLast("2");

		//其实调用的就是indexof
		//for node=first;node!=null;node=node.next
		//if node.item == 1
		//return
		dataList.contains("1");
		//node = node.pre
		dataList.lastIndexOf("2");
		dataList.indexOf("1");
	}
	
	@Test
	public void testPeek(){
		//peek等同于getFirst 只是当为空队列的时候返回Null
		//而getFirst空队列会NoSuchElementException
		LinkedList<String> dataList = new LinkedList<>();
		String first = dataList.peek();
		String first1 = dataList.peekFirst();
		System.out.println(first == first1);
		
		try {
			first = dataList.getFirst();
			System.out.println(first);
		} catch (Exception e) {
			System.out.println(e instanceof java.util.NoSuchElementException);
		}
		
		try {
			//等同于getFirst
			first = dataList.element();
			System.out.println(first);
		} catch (Exception e) {
			System.out.println(e instanceof java.util.NoSuchElementException);
		}
		
		dataList.getLast() ; //NoSuchElementException
		dataList.peekLast(); //null
	}
	
	@Test
	public void testPool(){
		//类似于removeFirst , 但是不会报错
		//removeFirst == remove
		LinkedList<String> dataList = new LinkedList<>();
		String first = dataList.poll();
		//等同 dataList.pollFirst()
		System.out.println(first);
		
		try {
			dataList.remove();
		} catch (Exception e) {
			System.out.println(e instanceof java.util.NoSuchElementException);
		}
//		dataList.removeFirst()
		dataList.pop();
	}
	
	@Test
	public void testOffer(){
		//等同效果
		LinkedList<String> dataList = new LinkedList<>();
		dataList.offer("1");
		dataList.add("2");
		dataList.addLast("3");
		dataList.offerLast("4");
		dataList.push("5");
		//等同效果
		dataList.offerFirst("0");
		dataList.addFirst("-1");
		System.out.println(dataList);
	}
	
	@Test
	public void removeFirstOccurrence(){
		LinkedList<String> dataList = new LinkedList<>();
		dataList.removeFirstOccurrence("");
		dataList.removeLastOccurrence("");
	}

}
